package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.builder.web.PageViewBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.request.rpc.AppRequest;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.JedisUtil;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.common.util.WxTemplateUtil;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplateDeleteRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplatePageQueryRequest;
import com.fulihui.wgj.admin.api.wxtemplate.vo.WxTemplateDataVO;
import com.fulihui.wgj.admin.api.wxtemplate.vo.WxTemplateVO;
import com.fulihui.wgj.admin.integration.WxTemplateServiceClient;
import com.fulihui.wgj.facade.dto.WxTemplateDTO;
import com.fulihui.wgj.facade.request.template.WxTemplateDeleteRequest;
import com.fulihui.wgj.facade.request.template.WxTemplatePageQueryRequest;
import com.fulihui.wgj.facade.request.template.WxTemplateQueryRequest;
import com.fulihui.wgj.facade.result.template.WxTemplatePageResult;
import com.fulihui.wgj.facade.service.template.WxTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: Levon
 * @date: 2018-07-25 17:43
 */
@Service
public class WxTemplateServiceClientImpl implements WxTemplateServiceClient {

    @Reference(version = "1.0.0")
    private WxTemplateService wxTemplateService;

    @Autowired
    private JedisUtil jedisUtil;

    private final static String LATELY = "LATELY_";

    @Override
    public Result<Boolean> syncTemplates(String appId) {
        AppRequest appRequest = new AppRequest();
        appRequest.setAppId(appId);
        BaseResult baseResult = wxTemplateService.syncTemplate(appRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public Result<List<String>> queryDeputyIndustry(String appId) {
        AppRequest appRequest = new AppRequest();
        appRequest.setAppId(appId);
        SimpleResult<List<String>> baseResult = wxTemplateService.queryDeputyIndustry(appRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(baseResult.getData());
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public PageView<WxTemplateVO> queryWxTemplates(TemplatePageQueryRequest request) {
        WxTemplatePageQueryRequest queryRequest = new WxTemplatePageQueryRequest();
        queryRequest.setDeputyIndustry(request.getDeputyIndustry());
        queryRequest.setAppId(request.getAppId());
        queryRequest.setRows(request.getRows());
        queryRequest.setPage(request.getPage());
        List<WxTemplateVO> wxTemplateVOS = null;
        WxTemplatePageResult pageResult = wxTemplateService.pageQueryTemplates(queryRequest);
        if (RpcResultCheckUtil.isSuccess(pageResult)) {
            List<WxTemplateDTO> values = pageResult.getValue();
            wxTemplateVOS = BeanConv.copy(values, WxTemplateVO.class);
        }
        return PageViewBuilder.build(wxTemplateVOS, pageResult.getPage(), pageResult.getRows(), pageResult.getTotalRows());
    }

    @Override
    public Result<Boolean> delTemplate(TemplateDeleteRequest request) {
        WxTemplateDeleteRequest deleteRequest = new WxTemplateDeleteRequest();
        deleteRequest.setId(request.getId());
        BaseResult baseResult = wxTemplateService.deleteTemplate(deleteRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public Result<WxTemplateDataVO> queryLately(String loginId) {
        String templateId = jedisUtil.get(LATELY + loginId);
        WxTemplateDataVO dataVO = querySingleTemplate(templateId);
        return JsonResultBuilder.succ(dataVO);
    }

    @Override
    public Result<WxTemplateDataVO> selectTemplate(String loginId, String templateId) {
        jedisUtil.set(LATELY + loginId, templateId);
        WxTemplateDataVO dataVO = querySingleTemplate(templateId);
        return JsonResultBuilder.succ(dataVO);
    }

    private WxTemplateDataVO querySingleTemplate(String templateId) {

        WxTemplateDataVO dataVO = new WxTemplateDataVO();
        if (StringUtils.isBlank(templateId)) {
            return null;
        }

        WxTemplateQueryRequest queryRequest = new WxTemplateQueryRequest();
        queryRequest.setTemplateId(templateId);
        SimpleResult<WxTemplateDTO> simpleResult = wxTemplateService.querySingleTemplate(queryRequest);
        if (RpcResultCheckUtil.isSuccess(simpleResult) && simpleResult.getData() != null) {
            WxTemplateDTO templateDTO = simpleResult.getData();
            Map<String, String> map = WxTemplateUtil.parseContent(templateDTO.getContent());
            dataVO.setTitle(templateDTO.getTitle());
            dataVO.setTemplate_id(templateDTO.getTemplateId());
            dataVO.setBody(map);
            return dataVO;
        }
        return null;
    }
}
